/**
 *
 */
package be.donovan.gui.helper.components;

import java.awt.Color;

import javax.swing.DefaultCellEditor;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.Highlighter;
import org.joda.time.YearMonthDay;
import org.joda.time.base.AbstractInstant;
import org.joda.time.base.AbstractPartial;




/**
 * Provides an unique point to custom all ATDL <code>JXTable</code>s
 */
public class JXTableDecorator {
	/**
	 * Background Color used to render a selected cell or a selected row
	 */
	public static Color selectionBackgroundColor = Color.yellow;
	/**
	 * Foreground Color used to render a selected cell or a selected row
	 */
	public static Color selectionForegroundColor = Color.black;


	/**
	 * Set style and editors for a JXTable
	 * @param table JXTable to custom
	 * @param tableName name of the JXTable
	 *
	 *
	 */
	public static void setStyleAndEditors(JXTable table, String tableName){
		table.setName(tableName);
		table.setColumnControlVisible(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setSelectionForeground(selectionForegroundColor);
		table.setSelectionBackground(selectionBackgroundColor);
		
//		table.setHighlighters(
//        		new HighlighterPipeline(
//        				new Highlighter[]{AlternateRowHighlighter.quickSilver}
//        				)
//        		);
		table.setDefaultRenderer(AbstractPartial.class, new DateTimeRenderer());
		table.setDefaultRenderer(AbstractInstant.class, new DateTimeRenderer());
		table.setDefaultRenderer(Double.class, new CurrencyRenderer());
		table.setDefaultEditor(Double.class, new DefaultCellEditor(new CurrencyTextField()));
		table.setDefaultEditor(YearMonthDay.class, new DatePickerTableCellEditor());
//		table.setDefaultEditor(Latitude.class, new CoordinatePartTableCellEditor(CoordinatePartTableCellEditor.LATITUDE));
//		table.setDefaultEditor(Longitude.class, new CoordinatePartTableCellEditor(CoordinatePartTableCellEditor.LONGITUDE));
//		table.setDefaultEditor(Color.class, new ColorTableCellEditor());
//		table.setDefaultRenderer(Color.class, new ColorTableCellRenderer());
//		table.setDefaultRenderer(Latitude.class, new CoordinateTableCellRenderer());
//		table.setDefaultRenderer(Longitude.class, new CoordinateTableCellRenderer());
	}
	/**
	 * Set style and editors for a JXTable into a JScrollPane
	 * @param table JXTable to custom
	 * @param tableName String name of the JXTable
	 * @param scheduleViewScrollPane JScrollPane where will be the table
	 * @param titledBorderText String text of the border
	 */
	public static void setStyleAndEditors(JXTable table, String tableName, JScrollPane scrollPane, String titledBorderText){
		setStyleAndEditors(table, tableName);
		setTitle(scrollPane, titledBorderText);
        scrollPane.setViewportView(table);
        loadPreferences(table, tableName);
	}

	/**
	 * Load preferences of a JXTable and register the JXTable in the
	 * JXTableRegistry
	 * 
	 * @param table
	 *            JXTable for which to load preferences
	 * @param tableName
	 *            String name of the JXTable
	 */
	private static void loadPreferences(JXTable table, String tableName) {
		TableConfiguration.loadTableConfiguration(table, tableName);
		JXTableRegistry.register(table);
	}
	/**
	 * Add an Highlighter to a JXTable
	 * @param table JXTable to add highlighter
	 * @param highlighter Highlighter to add
	 */
//	public static void addHighlighter(JXTable table, Highlighter highlighter){
//		table.getHighlighters().addHighlighter(highlighter);
//	}

	public static void setTitle(JScrollPane scrollPane, String titledBorderText){
        TitledBorder titledBorder = new TitledBorder(titledBorderText);
        titledBorder.setTitleFont(new java.awt.Font("Tahoma", 1, 11));
        scrollPane.setBorder(titledBorder);
	}
}
